#pragma once
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define ROW 9
#define COL 9
#define EASY_COUNT 10

#define ROWS  ROW+2
#define COLS  COL+2
#define _CRT_SECURE_NO_WARNINGS

void menu() {
	printf("**********************\n");
	printf("*****  1.play  *******\n");
	printf("*****  0.exit  *******\n");
	printf("**********************\n");
}
void game() {
	//创建数组
	char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
	char show[ROWS][COLS] = { 0 };//存放排查好的雷的信息

	//初始化mine数组为全字符'0'
	InitBoard(mine, ROWS, COLS, '0');
	//初始化show数组为全'*'
	InitBoard(show, ROWS, COLS, '*');
	//打印棋盘

	DisplayBoard(show, ROW, COL);//只打印9*9的内容

	//布置雷
	SetMine(mine, ROW, COL);
	/*DisplayBoard(mine, ROW, COL);*/
	//这是不给玩家看到的

	//排查雷
	FindMine(mine, show, ROW, COL); //从mine中排查放到show中
}


void test() {
	int input = 0;
	srand((unsigned int)time(NULL));
	do {
		menu();
		printf("请选择:\n");
		scanf("%d", &input);
		switch (input) {
			case 1:
				//扫雷
				game();
				break;
			case 0:
				break;
			default:
				printf("选择错误");
				break;

		}
	} while (input);


}


int main() {
	test();
	return 0;
}
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) {
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++) {
		for (j = 0; j < cols; j++) {
			board[i][j] = set;
		}
	}

}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col) {
	//1-9
	int i = 0;
	int j = 0;
	printf("\n");
	printf("---扫雷游戏---\n");
	//打印列号
	for (i = 0; i <= col; i++) {
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++) {
		printf("%d ", i);
		for (j = 1; j <= col; j++) {

			printf("%c ", board[i][j]);
		}
		printf("\n");
	}

}

//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col) {
	//布置10个雷
	int count = EASY_COUNT;
	while (count) {
		//生产随机的下标
		int x = rand() % row + 1;//范围1到9
		int y = rand() % col + 1;
		if (mine[x][y] == '0') {
			mine[x][y] = '1';
			count--;
		}
	}
}

int get_mine_count(char mine[ROWS][COLS], int x, int y) {
	return mine[x - 1][y] +
	       mine[x - 1][y - 1] +
	       mine[x][y - 1] +
	       mine[x + 1][y - 1] +
	       mine[x + 1][y + 1] +
	       mine[x + 1][y] +
	       mine[x][y + 1] +
	       mine[x - 1][y + 1] - 8 * '0';
}
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
	//1.输入排查雷的坐标
	//2.检查该坐标是不是雷
	//（1）是雷    --> 很遗憾炸死了
	//（0）不是雷  --> 统计坐标周围有几个雷-->存储排查雷的信息到show数组，游戏继续
	int x = 0;
	int y = 0;
	int win = 0;
	while (win < row * col - EASY_COUNT) {
		printf("请输入要排查的坐标： ");
		scanf("%d %d", &x, &y);//x的范围是1～9，y的范围是1～9
		//判断坐标的合法性
		if (x >= 1 && x <= col && y >= 1 && y <= row) {
			if (mine[x][y] == '1') {
				printf("很遗憾，你被炸死了\n");
				DisplayBoard(mine, row, col);//把怎么被炸死的显现出来
				break;
			} else {
				//不是雷的话统计(x,y)坐标周围有几个雷
				int count = get_mine_count(mine, x, y);
				show[x][y] = count + '0';
				//显示排查出的信息
				DisplayBoard(show, row, col);
				win++;
			}
		} else {
			printf("坐标不合法，请重新输入\n");
		}
	}
	if (win == row * col - EASY_COUNT) {
		printf("恭喜你，排雷成功!");
		DisplayBoard(mine, row, col);
	}


}

//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
